package com.pjedu.springbootthymeleaf10.web;

import com.pjedu.springbootthymeleaf10.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 汪洋
 * @Description
 */
@Controller
public class ThymeleafController {
    @RequestMapping("/t1")
    public String t1(Model model){
        model.addAttribute("url","http://www.taobao.com");
        model.addAttribute("flag",true);
        return "1.ifUnlessDemo";
    }

    @RequestMapping("/t2")
    public String t2(Model model){
        ArrayList<User> list = new ArrayList<>();
        Collections.addAll(list,
                User.builder().uid(1001).username("zhangsan").password("111111").build(),
                User.builder().uid(1002).username("lisi").password("222222").build(),
                User.builder().uid(1003).username("wangwu").password("333333").build()
        );
        model.addAttribute("list",list);
        return "2.forDemo";
    }

    @RequestMapping("/t3")
    public String t3(Model model){
        Map<String, Object> map = new HashMap<>();
        map.put("name","zhangsan");
        map.put("age",19);
        map.put("sex","male");
        model.addAttribute("map",map);
        return "2.forDemo";
    }

    @RequestMapping("/t4")
    public String t4(Model model, HttpSession session, HttpServletRequest request){
        ArrayList<User> list = new ArrayList<>();
        Collections.addAll(list,
                User.builder().uid(1001).username("zhangsan").password("111111").build(),
                User.builder().uid(1002).username("lisi").password("222222").build(),
                User.builder().uid(1003).username("wangwu").password("333333").build()
        );
        model.addAttribute("list",list);
        model.addAttribute("country","中国");
        model.addAttribute("color","blue");
        session.setAttribute("color","red");
        request.getServletContext().setAttribute("color","yellow");
        return "3.neilian";
    }
}
